package com.vogue.supplymall.payment;

import com.vogue.common.sqlparse.druid.visitor.MySqlPartitionOutputVisitor;
import com.vogue.configs.startup.SpringContext;
import com.vogue.configs.startup.SpringStart;
import com.vogue.gear.memory.provider.annotation.MemorySchemeP2P;
import com.vogue.ssm.database.entity.SnowflakeIdWorker;
import com.vogue.supplymall.common.config.CurrencyPropertiesConfig;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@EnableTransactionManagement
@EnableAsync
@MemorySchemeP2P
@Import(SpringContext.class)
public class PaymentApplication 
{
	public static void main(String[] args) {
        Map<String,String> mapargs=transargs(args);
        if(mapargs.size()>0){
            String s1=mapargs.get("serno");//所在服务器编号
            if(StringUtils.isNotEmpty(s1)){
                SnowflakeIdWorker.serno=Integer.parseInt(s1);
            }
            s1=mapargs.get("dsno");//同一服务器中服务的编号
            if(StringUtils.isNotEmpty(s1)){
                SnowflakeIdWorker.dsno=Integer.parseInt(s1);
            }
        }
        ConfigurableApplicationContext context = SpringStart.run(PaymentApplication.class,args);
        String tableName = context.getBean(CurrencyPropertiesConfig.class).getPlatTables();
        if(tableName != null) {
        	//注明哪些表格是要分区查询的
        	MySqlPartitionOutputVisitor.setCanShade(tableName.split(","));
        }
    }

    private static Map<String,String> transargs(String[] args){
        Map<String,String> mapargs=new HashMap<>();
        if(args!=null){
            for (String arg:args){
                if (arg.startsWith("--")){
                    int pos=arg.indexOf("=");
                    if(pos>0){
                        mapargs.put(arg.substring(2,pos),arg.substring(pos+1));
                    }
                }
            }
        }
        return mapargs;
    }
}
